import request from '@/utils/request'
import type { ApiResponse } from '@/types'

// 验证码相关接口

/**
 * 生成验证码
 */
export function generateCaptcha(): Promise<ApiResponse<{
  captchaId: string
  expireTime: number
  code?: string // 开发环境可能返回
}>> {
  return request.get('/captcha/generate')
}

/**
 * 验证验证码
 */
export function validateCaptcha(captchaId: string, code: string): Promise<ApiResponse<string>> {
  return request.post('/captcha/validate', null, {
    params: { captchaId, code }
  })
}

/**
 * 刷新验证码
 */
export function refreshCaptcha(captchaId: string): Promise<ApiResponse<{
  captchaId: string
  expireTime: number
  code?: string
}>> {
  return request.post('/captcha/refresh', null, {
    params: { captchaId }
  })
}

/**
 * 获取验证码状态
 */
export function getCaptchaStatus(captchaId: string): Promise<ApiResponse<{
  exists: boolean
  expireTime: number
}>> {
  return request.get('/captcha/status', {
    params: { captchaId }
  })
}

/**
 * 获取验证码图片URL
 */
export function getCaptchaImageUrl(captchaId: string): string {
  return `/api/captcha/image?captchaId=${captchaId}&t=${Date.now()}`
}
